package com.atguigu.guli.gmall.gateway.filter;

import lombok.Data;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Arrays;
import java.util.List;

/*
    Test2GatewayFilterFactory命名时如果使用了XxxGatewayFilterFactory那么GatewayFilterFactory之前的字符串就是
    当前过滤器的名称

    我们也可以通过 name方法重写，返回执行的过滤器的名称
 */
//@Component
public class Test2GatewayFilterFactory extends AbstractGatewayFilterFactory<Test2GatewayFilterFactory.Config> {
    public Test2GatewayFilterFactory(){
        super(Config.class);
    }
    @Override
    public GatewayFilter apply(Config config) {
        return new GatewayFilter() {//返回一个过滤器对象
            //过滤请求的方法
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
                System.out.println(config.getPaths());
                System.out.println("局部过滤器过滤了请求.......");

                return chain.filter(exchange);
            }
        };
    }

    @Override
    public ShortcutType shortcutType() {
        return ShortcutType.GATHER_LIST;
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Arrays.asList("paths");
    }

    //    @Override
//    public String name() {
//        return null;
//    }
    @Data
    public static class Config{
        private List<String> paths;
    }

}
